// import {canvas} from "../../js/libs/weapp-adapter";
import Button from "./Button";

const ctx = canvas.getContext('2d');


export default class MyApp{

    constructor() {

        this.frame = 0;

        // btn
        this.btn_show = new Button(ctx,{
            img:"src/main/imgs/btn_1.png",
            width:128,
            height:64,
            x:50,
            y:50,
            text:'显示排行榜'
            });

        this.btn_hide = new Button(ctx,{
            img:"src/main/imgs/btn_1.png",
            width:128,
            height:64,
            x:200,
            y:50,
            text:'隐藏排行榜'
        });

        console.log("hello world!");
        console.log(canvas);

        // 添加按钮点击事件
        canvas.addEventListener('touchstart',this.touchHandler.bind(this));

        //  开始游戏循环
        this.bindLoop = this.loop.bind(this);
        window.requestAnimationFrame(this.bindLoop,canvas);
    }

    touchHandler(e){
        e.preventDefault();

        const x = e.touches[0].clientX;
        const y = e.touches[0].clientY;
        console.log(`x:${x},y:${y}`);

        // 判断按钮是否被点击
        if (this.btn_show.containPoint(x,y)){
            this.showRank();
        }
        if (this.btn_hide.containPoint(x,y)){
            this.hideRank();
        }
    }

    loop(){
        this.frame++;

        // update
        this.update();
        this.render();

        //render
        window.requestAnimationFrame(this.bindLoop,canvas);
    }

    update(){}
    render(){
        this.btn_show.render(ctx);
        this.btn_hide.render(ctx);
    }


    showRank(){
        let openDataContext = wx.getOpenDataContext()
        openDataContext.postMessage({
            command:'showRank'
        })
    }

    hideRank(){
        let openDataContext = wx.getOpenDataContext()
        openDataContext.postMessage({
            command:'hideRank'
        })
    }
}